#include "PackageModule.h"

PackageModule::PackageModule()
{
	
}

void PackageModule::readPackageData()
{
	vector<string> usr_str;
	string tmp_str = "";
	vector<string> vec_str;

	ifstream userFile("PACKAGE.dat", ios::in);

	if(userFile.is_open()) {
		while(userFile.good()) {
			getline(userFile, tmp_str);
			if(tmp_str == "") {
				break;
			}

			usr_str.push_back(tmp_str);

		}
	} else {
		cout << "Error opening user file" << endl;
	}

	for(vector<int>::size_type i = 0; i != usr_str.size(); i++) {
		stringstream ss(usr_str[i]);
		tmp_str = "";

		vec_str.clear();
		while(getline(ss, tmp_str, ',')) {
			vec_str.push_back(tmp_str);
		}

		package_list.push_back(Package(vec_str[0], vec_str[1], vec_str[2]));

	}

	userFile.close();
}

void PackageModule::writePackageData()
{
	ofstream fileStream("PACKAGE.dat", ios::out);

	if (fileStream.is_open()) {

		for(vector<int>::size_type i = 0; i < package_list.size(); i++) {
			fileStream << package_list[i].getPackId() << "," << package_list[i].getPackName() << "," << package_list[i].getPackDuration() << endl;
		}

	} else {
		cout << "Unable to open data file" << endl;
	}

	fileStream.close();
}

void PackageModule::addNewPackage()
{
	string tmp = "";
	int tmp_packid = package_list.size();
	tmp_packid++;

	cout << "********** Add New Holiday Package **********" << endl;
	Package newHolidayPackage;
	cout << "Enter Holiday Package Name: ";
	
	getline(cin, tmp);
	newHolidayPackage.setPackName(tmp);

	cout << "Enter Holiday Package Duration: ";
	cin >> tmp;
	cin.ignore();
	newHolidayPackage.setPackDuration(tmp);

	newHolidayPackage.setPackId(tmp_packid);

	package_list.push_back(newHolidayPackage);

}

void PackageModule::deletePackage()
{
	string pDelete = "";
	cout << "********* Delete Existing Holiday Package *********" << endl;
	cout << "Enter Name of Holiday Package: ";
	getline(cin, pDelete);

	for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
		if( (*it).getPackName() == pDelete ) {
			package_list.erase(it);
			cout << pDelete << " removed" << endl;
			break;
		}
	}
}

void PackageModule::editPackage()
{
	string usr_input = "";
	bool found = false;
	cout << "*********** Edit Existing Holiday Package **********" << endl;
	cout << "Enter Name of Holiday Package: ";
	getline(cin, usr_input);

	for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
		if( (*it).getPackName() == usr_input ) {
			found = true;

		}
	}

	if(found) {
		string newName = "";
		string newDuration = "";
		cout << "Enter New Name of Holiday Package: ";
		getline(cin,newName);
		cout << "Enter New Duration of Holiday Package: ";
		getline(cin,newDuration);

		for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
			if( (*it).getPackName() == usr_input ) {
				(*it).setPackName(newName);
				(*it).setPackDuration(newDuration);
			}
		}
	} else {
		cout << "The Holiday Package is not found" << endl;
	}

}

void PackageModule::showPackage()
{
	for(vector<Package>::iterator it = package_list.begin(); it != package_list.end(); ++it) {
		cout << (*it).getPackId() << " . " << (*it).getPackName() << " . "
		<< (*it).getPackDuration() << endl;
	}
}